<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输角色名称" clearable v-model="name" @clear="searchRole">
                        <el-button slot="append" icon="el-icon-search" @click="searchRole"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addDialogVisible=true">添加角色</el-button>
                </el-col>
            </el-row>
            <el-table :data="roleList" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="角色名称" prop="name"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="showEditDialog(scope.row)">修改</el-button>
                        <el-button type="danger" size="mini" @click="removeRoleById(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[2, 4 , 6, 8]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
            ></el-pagination>
        </el-card>
        <!--添加角色-->
        <el-dialog title="添加角色" :visible.sync="addDialogVisible" width="50%" @close="addRoleFormClose">
            <el-form
                    :model="addRoleForm"
                    ref="addRoleFormRef"
                    label-width="100px"
                    class="demo-ruleForm"
            >
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="addRoleForm.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="addRole">确 定</el-button>
                <el-button @click="addDialogVisible = false">取 消</el-button>
            </span>
        </el-dialog>
        <!--修改角色-->
        <el-dialog
                title="修改角色"
                :visible.sync="editDialogVisible"
                width="50%"
                @close="editRoleFormClose"
        >
            <el-form
                    :model="editRoleForm"
                    ref="editRoleFormRef"
                    label-width="100px"
                    class="demo-ruleForm"
            >
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="editRoleForm.name"></el-input>
                </el-form-item>
                <el-tree
                        :data="treedata"
                        :check-strictly="true"
                        show-checkbox
                        default-expand-all
                        node-key="id"
                        ref="tree"
                        highlight-current
                        :props="defaultProps">
                </el-tree>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="editRole">确 定</el-button>
                <el-button @click="editDialogVisible = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                addDialogVisible: false,//表示添加窗口是显示，还是隐藏
                editDialogVisible: false,//表示更新窗口是显示，还是隐藏
                pageNum: 1,
                pageSize: 2,
                name: "",
                roleList: [],
                total: 0,
                treedata: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                addRoleForm: {
                    name: ""
                },
                editRoleForm: {
                    id: -1,
                    name: "",
                    permString:''
                }
            };
        },
        methods: {
            reloadTreeData(){
                this.$axios({
                    url:this.$baseurl.dev+'manager/queryPermissionTreeLevel1',
                    method:'get'
                }).then(res=>{
                    console.log('reloadTreeData',res)
                    this.treedata=res.data.data
                })
            },
            addRoleFormClose(){

            },
            // 删除角色信息
            removeRoleById(id) {
                let that = this;
                that.$confirm('确认删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    that.$axios({
                        url:this.$baseurl.dev+'manager/deleteByRoleId?id='+id,
                        method:'post'
                    }).then(res=>{
                        if(res.data.code==200) {
                            that.getRoleList();
                            that.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else{
                            that.$message({
                                message: res.data.message,
                                type: 'error',
                                duration: 1000
                            });
                        }

                    })


                }).catch(() => {
                    that.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            //添加角色
            showAddDialog(row) {
                this.addDialogVisible = true;
            },
            //编辑角色
            showEditDialog(row) {
                this.editDialogVisible = true;
                this.$axios({
                    url:this.$baseurl.dev+'manager/getRoleAndPermissionsById?id='+row.id,
                    method:'get'
                }).then(res=>{
                    if(res.data.code==200){
                        this.editRoleForm.id=res.data.data.id;
                        this.editRoleForm.name=res.data.data.name;
                        this.$refs.tree.setCheckedKeys(res.data.data.permIdList);
                    }else{
                        this.$message({
                            message: res.data.message,
                            type: 'error',
                            duration: 1000
                        });
                    }
                })
            },
            editRoleFormClose() {
                this.$refs.editRoleFormRef.resetFields();
            },


            editRole() {
                let permArray=this.$refs.tree.getCheckedKeys();
                if(permArray){
                    this.editRoleForm.permString=permArray.join(",")
                }
                this.$axios({
                    url:this.$baseurl.dev+'manager/editRole',
                    method:'post',
                    data:{
                        id: this.editRoleForm.id,
                        name: this.editRoleForm.name,
                        permString:this.editRoleForm.permString
                    }
                }).then(res=>{
                    if(res.data.code==200){
                        this.editRoleForm.name='';
                        this.editRoleForm.permString='';
                        this.editDialogVisible=false;
                        this.getRoleList();
                    }
                })
            },
            addRole() {
                this.$axios({
                    url:this.$baseurl.dev+'manager/addRole',
                    method:'post',
                    data:this.addRoleForm
                }).then(res=>{
                    if(res.data.code==200){
                        this.addRoleForm.name='';
                        this.addDialogVisible=false;
                        this.getRoleList();
                    }
                })
            },
            //读取角色列表
            getRoleList() {
                this.$axios({
                    url:this.$baseurl.dev+'manager/queryRoleWithPage',
                    method:'get',
                    params:{
                        pageNum:this.pageNum,
                        pageSize:this.pageSize,
                        name:this.name
                    }
                }).then(res=>{
                    if(res.data.code==200){
                        console.log('getRoleList',res)
                        this.roleList=res.data.data.list;
                        this.total=res.data.data.total;
                    }else{
                        this.$message({
                            message: res.data.message,
                            type: 'error',
                            duration: 1000
                        });
                    }
                })
            },
            searchRole() {
                this.pageNum = 1;
                this.pageSize = 2;
                this.getRoleList();
            },
            handleSizeChange(newSize) {
                this.pageSize = newSize;
                this.getRoleList();
            },
            handleCurrentChange(newPage) {
                this.pageNum = newPage;
                this.getRoleList();
            }

        },
        created() {
            //分页查询角色列表
            this.searchRole();
            this.reloadTreeData();
        }
    };
</script>
<style lang="less" scoped>
</style>
